package com.whmnx.chitchat.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.bean.ChatMsgBean;
import com.whmnx.chitchat.bean.LoginBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.event.EventConstants;
import com.whmnx.chitchat.event.MessageEvent;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.ui.chat.ChatActivity;
import com.whmnx.chitchat.utils.ImageUtil;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;


/**
 * author : huang
 * date   : 2019/10/21 0021 15:06
 * version: 1.0
 * @author Administrator
 */
public class ChatListAdapter2 extends CommonRecyclerAdapter<ChatMsgBean> {

    private Context context;
    public ChatListAdapter2(Context context) {
        super(context, R.layout.item_chat_list2);
        this.context = context;
    }


    @Override
    public void onUpdate(BaseAdapterHelper helper, ChatMsgBean item, int position) {  //SendUserNickName
        String nickName =  SPUtil.getString(Constants.NICKNAME);
        ImageUtil.setImage(helper.getView(R.id.conversation_icon), (String) item.SendUserHeadImg);
        helper.setText(R.id.conversation_title, (String) item.SendUserNickName);
        if (TextUtils.isEmpty(item.ChatRecord_Content)){
            if (item.getContent().contains("##")){
                helper.setText(R.id.conversation_last_msg,"[文件]");
            }else if (item.getContent().contains(".png") || item.getContent().contains(".jpg")){
                helper.setText(R.id.conversation_last_msg,"[图片]");
            }else if (item.getContent().contains("http")){
                helper.setText(R.id.conversation_last_msg,"[语音]");
            }else if (item.getContent().contains(nickName)){
                String s = item.getContent().replace(nickName, "您");
                helper.setText(R.id.conversation_last_msg, s);
            }else if (item.getContent().contains("[")){
                helper.setText(R.id.conversation_last_msg,"[表情]");
            }else {
                helper.setText(R.id.conversation_last_msg,item.getContent());
            }
        }else {
            helper.setText(R.id.conversation_last_msg,item.ChatRecord_Content);
        }

        helper.setText(R.id.conversation_time,item.getCreateTime());
        if (item.getUnreadNum() > 0){
            helper.setVisible(R.id.unread_msg_number,View.VISIBLE);
            helper.setText(R.id.unread_msg_number,item.getUnreadNum() + "");
        }else {
            helper.setVisible(R.id.unread_msg_number,View.GONE);
        }


       /* helper.getView(R.id.btnDelete).setOnClickListener(view -> {
            Map<String, Object> params = new HashMap<>();
            params.put("UserID", SPUtil.getString(Constants.USERID));
          //  params.put("ChatRecordIDs",item.getChatRecordID());
            int type = item.getChatRecordRecordType() == 0 ? 0 : 1;
            params.put("RecordType",type + "");
            if (item.getType() == 1){
                params.put("GroupID",item.getObjectID());
            }else {
                params.put("ToUserID",item.getObjectID());
            }
            HttpClient.get(context, Constants.GETDELETECHATRECORD, params,new CallBack<LoginBean>() {
                @Override
                public void onSuccess(LoginBean result) {
                    UItils.showToastSafe("删除消息列表成功");
                    getData().remove(position);
                    notifyItemRemoved(position);
                    if (position != getData().size()) {
                        notifyItemRangeChanged(position, getData().size() - position);
                    }
                    MessageEvent messageEvent = new MessageEvent();
                    messageEvent.setMessageKey(EventConstants.SENDMSG);
                    EventBus.getDefault().post(messageEvent);
                }
            });
        });*/

        helper.getView(R.id.item_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SPUtil.put(Constants.FORMPHOTO,item.getImg());
                ChatInfo chatInfo = new ChatInfo();
              /*  chatInfo.setType(item.getType() == 1 ? TIMConversationType.Group : TIMConversationType.C2C);
                chatInfo.setId(item.getObjectID());
                chatInfo.setChatName(item.getName());
                chatInfo.setTopChat(item.getChatRecordRecordType() == 1 ? true : false);
                Intent intent = new Intent(context, ChatActivity.class);
                intent.putExtra(Constants.CHAT_INFO, chatInfo);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);*/



                if (!TextUtils.isEmpty(item.getName())){
                    chatInfo.setChatName(item.getName());
                }else {
                    String nick = SPUtil.getString(Constants.NICKNAME);
                    if (!TextUtils.isEmpty(item.SendUserNickName) && item.SendUserNickName.equals(nick)){
                        chatInfo.setChatName(item.ToUserNickName);
                        //   chatInfo.setId(item.ChatRecord_UserID);
                    }else {
                        chatInfo.setChatName(item.SendUserNickName);
                        //  chatInfo.setId(item.ChatRecord_UserID);
                    }
                }

                if (!TextUtils.isEmpty((String) item.getObjectID())){
                    chatInfo.setId(item.getObjectID());
                    chatInfo.setType(item.getType() == 1 ? TIMConversationType.Group : TIMConversationType.C2C);
                }else {
                    if (!TextUtils.isEmpty(item.ChatRecord_GroupID)){
                        chatInfo.setId(item.ChatRecord_GroupID);
                        chatInfo.setType(TIMConversationType.Group);
                        chatInfo.setChatName("");
                    }else {
                        chatInfo.setId(item.ChatRecord_UserID);
                        chatInfo.setType(TIMConversationType.C2C);
                    }
                }

                // chatInfo.setChatName(item.getName());
             //   chatInfo.setTopChat(item.getChatRecordRecordType() == 1 ? true : false);
                if (TextUtils.isEmpty(item.ChatRecordRecordType)){
                    chatInfo.setTopChat(item.getChatRecordRecordType() == 1 ? true : false);
                }else {
                    int type = Integer.parseInt(item.ChatRecordRecordType);
                    chatInfo.setTopChat(type == 1 ? true : false);
                }
                Intent intent = new Intent(context, ChatActivity.class);
                intent.putExtra(Constants.CHAT_INFO, chatInfo);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
    }
}

